//
//  SoundDetailViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 lushan. All rights reserved.
//

#import "SoundDetailViewController.h"
#import "SoundDetailHeadView.h"
#import "SoundDetailCell.h"
#import "LSAppTools.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "SoundViewController.h"

@interface SoundDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *soundDetailTableView;
@property (nonatomic, retain)SoundDetailHeadView *detailView;
@property (nonatomic, assign)NSInteger nextPage;
@property (nonatomic, copy)NSString *contentId;
@property (nonatomic, assign)BOOL isUpLoad;
@property (nonatomic, retain)NSMutableArray *soundDetailArray;
@property (nonatomic, copy)NSString *url;
@property (nonatomic, copy)NSString *body;
@property (nonatomic, retain)NSMutableArray *soundArray;

@end

@implementation SoundDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectZero];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:self.titleNss];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:20];
    [customLab sizeToFit];
    self.navigationItem.titleView = customLab;
    
    self.soundDetailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 48) style:UITableViewStylePlain];
    [self.view addSubview:self.soundDetailTableView];
    
    self.detailView = [[SoundDetailHeadView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, HEIGHT / 2.3)];
    self.soundDetailTableView.tableHeaderView = self.detailView;
    
    self.soundArray = [[NSMutableArray alloc]init];
    self.soundDetailTableView.delegate = self;
    self.soundDetailTableView.dataSource = self;
    //注册
    [self.soundDetailTableView registerClass:[SoundDetailCell class] forCellReuseIdentifier:@"cell"];
    
    // 上拉/下拉刷新
    [self addHeaderRefresh];
    [self addFooterRefresh];
    
    self.soundDetailArray = [[NSMutableArray alloc] init];
    //标记操作
    self.isUpLoad = NO;
}

// 设置下拉刷新
-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    self.soundDetailTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        // 记录页码的
        self.nextPage = 0;
        //标记为下拉操作
        self.isUpLoad = NO;
        
        self.url = @"http://api2.pianke.me/ting/radio_detail";
        self.body = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&radioid=%@&version=3.0.6", self.soundid];
        
        [LSAppTools postUrl:self.url body:self.body result:LHJSON requestStyle:LHRequestString headerFile:nil success:^(id result) {
            if (self.isUpLoad == NO) {
                //说明是下拉，就要清空数组中的数据
                [self.soundDetailArray removeAllObjects];
            }
            NSMutableDictionary *bigDic = [result objectForKey:@"data"];
            self.soundArray = [bigDic objectForKey:@"list"];
            
            for (NSMutableDictionary *dic in [bigDic objectForKey:@"list"]) {
                
                SoundDetailModel *model = [[SoundDetailModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.soundDetailArray addObject:model];
                
            }
            self.detailView.titleLabel.text = [[bigDic objectForKey:@"radioInfo"] objectForKey:@"title"];
            self.detailView.briefLabel.text = [[bigDic objectForKey:@"radioInfo"] objectForKey:@"desc"];
            self.detailView.countLabel.text = [NSString stringWithFormat:@"%@", [[bigDic objectForKey:@"radioInfo"] objectForKey:@"musicvisitnum"]];
            NSURL *url = [NSURL URLWithString:[[bigDic objectForKey:@"radioInfo"] objectForKey:@"coverimg"]];
            [self.detailView.headImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"holderImage.jpg"]];
            NSURL *userUrl = [NSURL URLWithString:[[[bigDic objectForKey:@"radioInfo"] objectForKey:@"userinfo"] objectForKey:@"icon"]];
            [self.detailView.pic sd_setImageWithURL:userUrl placeholderImage:[UIImage imageNamed:@"holderImage.jpg"]];
            
            [self.soundDetailTableView.mj_footer endRefreshing];
            [self.soundDetailTableView.mj_header endRefreshing];
            [self.soundDetailTableView reloadData];
            
        } failure:^(NSError *error) {
            
        }];
        // 重新请求数据
        [self.soundDetailTableView reloadData];
    }];
    // 进到应用自动刷新(可以放在viewdidload)
    [self.soundDetailTableView.mj_header beginRefreshing];
}
// 设置上拉刷新
-(void)addFooterRefresh
{
    //当上拉时候触发该方法
    self.soundDetailTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        //每次上拉刷新10条
        self.nextPage += 10;
        self.isUpLoad = YES;
        
        self.url = @"http://api2.pianke.me/ting/radio_detail_list";
        self.body = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&limit=10&radioid=54b5d8bb8ead0e08180000f3&start=%ld&version=3.0.6", self.nextPage];
        
        [LSAppTools postUrl:self.url body:self.body result:LHJSON requestStyle:LHRequestString headerFile:nil success:^(id result) {
            
            NSMutableDictionary *bigDic = [result objectForKey:@"data"];
            self.soundArray = [bigDic objectForKey:@"list"];
            
            for (NSMutableDictionary *dic in [bigDic objectForKey:@"list"]) {
                
                SoundDetailModel *detailModel = [[SoundDetailModel alloc] init];
                [detailModel setValuesForKeysWithDictionary:dic];
                [self.soundDetailArray addObject:detailModel];
                
            }
            [self.soundDetailTableView.mj_footer endRefreshing];
            [self.soundDetailTableView.mj_header endRefreshing];
            [self.soundDetailTableView reloadData];
            
        } failure:^(NSError *error) {
            
            
        }];
        
        // 重新请求数据
        [self.soundDetailTableView reloadData];
    }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.soundDetailArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    SoundDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    // 取消点击cell的效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.soundDetailModel = [self.soundDetailArray objectAtIndex:indexPath.row];
    return cell;
}

//点击cell方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //播放状态改为1
    SoundDetailModel *model = [self.soundDetailArray objectAtIndex:indexPath.row];
    [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"play"];
    
    [self.navigationController popViewControllerAnimated:YES];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"musicid" object:model];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
